package com.service.impl;

import com.dao.MemberDao;
import com.dao.impl.MemberDaoImpl;
import com.entity.Member;
import com.result.QueryResult;
import com.service.MemberService;
import com.util.DBUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MemberServiceImpl implements MemberService {
    private MemberDao memberDao=new MemberDaoImpl();
    @Override
    public Member add(Member member) throws SQLException, ClassNotFoundException {
        Integer insert = memberDao.insert(member);
        // 查询此次添加后生成的id
        String lastInsertId = "select LAST_INSERT_ID()";
        PreparedStatement preparedStatement = DBUtil.createPreparedStatement(lastInsertId);
        ResultSet resultSet = DBUtil.execQuery(preparedStatement);
        if(resultSet.next()){
            member.setNum(resultSet.getInt(1));
        }
        return member;
    }

    @Override
    public QueryResult selectByPage(Integer offset, Integer size) {
        return memberDao.selectByPage(offset,size);
    }

    @Override
    public void update(Member member) {
        memberDao.update(member);
    }

    @Override
    public Integer delete(Integer Num) {
        return memberDao.delete(Num);
    }

    @Override
    public QueryResult selectByName(String name) {


            return memberDao.selectByName(name);


    }
}
